package com.ruoyi.biz.attchment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.biz.attchment.domain.BizAttachment;
import com.ruoyi.biz.attchment.service.IBizAttachmentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.ValidType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 附件Controller
 *
 * @author Fei.Cong
 * @date 2023-03-28
 */
@RestController
@RequestMapping("/attachment/attachment")
public class BizAttachmentController extends BaseController {

    @Autowired
    private IBizAttachmentService bizAttachmentService;

    @Autowired
    ServerConfig serverConfig;

    /**
     * 查询附件列表
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:list')")
    @GetMapping("/list")
    public AjaxResult list(BizAttachment bizAttachment) {
        LambdaQueryWrapper<BizAttachment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return success(bizAttachmentService.list(lambdaQueryWrapper));
    }

    /**
     * 分页查询附件列表
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:list')")
    @GetMapping("/page")
    public TableDataInfo page(BizAttachment bizAttachment) {
        return getDataTable(bizAttachmentService.pageList(bizAttachment, startPage()));
    }

    /**
     * 导出附件列表
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:export')")
    @Log(title = "附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizAttachment bizAttachment) {
        LambdaQueryWrapper<BizAttachment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<BizAttachment> list = bizAttachmentService.list(lambdaQueryWrapper);
        ExcelUtil<BizAttachment> util = new ExcelUtil<BizAttachment>(BizAttachment.class);
        util.exportExcel(response, list, "附件数据");
    }

    /**
     * 获取附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizAttachmentService.getById(id));
    }

    /**
     * 获取附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:pictures')")
    @PostMapping(value = "/list/pictures")
    public AjaxResult getInfo(@RequestBody BizAttachment bizAttachment) {
        List<BizAttachment> attachmentList = bizAttachmentService.list(new LambdaQueryWrapper<BizAttachment>()
                .eq(BizAttachment::getApplyId, bizAttachment.getApplyId())
                .eq(BizAttachment::getAttachmentType, bizAttachment.getAttachmentType())
                .eq(BizAttachment::getValid, ValidType.VALID.getCode()));
        attachmentList.stream().forEach(v -> v.setFilePath(serverConfig.getUrl() + v.getFilePath()));
        return success(attachmentList);

    }

    /**
     * 新增附件
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:add')")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizAttachment bizAttachment) {
        return toAjax(bizAttachmentService.save(bizAttachment));
    }

    /**
     * 修改附件
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:edit')")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizAttachment bizAttachment) {
        return toAjax(bizAttachmentService.updateById(bizAttachment));
    }

    /**
     * 删除附件
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:remove')")
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizAttachmentService.removeBatchByIds(ids));
    }

}
